package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;


    @Override
    public ApartmentItemVo getApartmentItemVo(Long id) {
        //1.公寓基本信息
        ApartmentInfo apartmentInfo = getById(id);
        //2.公寓标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.getAptLabelInfoList(id);
        //3.公寓图片信息
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId, id));
        //4.公寓最小房租
        BigDecimal minRent = roomInfoMapper.getMinRent(id);

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);

        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //1.公寓基本信息
        ApartmentInfo apartmentInfo = getById(id);
        //2.公寓图片信息
        List<GraphVo> graphVoList = graphInfoMapper.getGraphVoList(ItemType.APARTMENT, id);
        //3.公寓标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.getAptLabelInfoList(id);
        //4.公寓配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoList(id);
        //5.公寓最小房租
        BigDecimal minRent = roomInfoMapper.getMinRent(id);
        //6.公寓信息是否被删除
        Byte isDeleted = apartmentInfo.getIsDeleted();

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setIsDelete(isDeleted == 1);

        return apartmentDetailVo;
    }
}




